package cn.fyg.service.finance.domain.salary.impl;

import java.io.StringReader;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader;

import cn.fyg.common.base.retv.Retv;
import cn.fyg.service.finance.config.Connect;
import cn.fyg.service.finance.domain.salary.ProjectDto;
import cn.fyg.service.finance.domain.salary.SalaryDto;
import cn.fyg.service.finance.domain.salary.SalaryService;
import cn.fyg.service.finance.domain.salary.SalarydetailDto;
import cn.fyg.service.finance.domain.salary.SearchBean;
import cn.fyg.service.finance.util.DES;
import cn.fyg.service.finance.util.GsonUtil;

@Component
public class SalaryServiceImpl implements SalaryService{
	public static final String REST_SERVICE_URI = new Connect().getUrl();
	
	private String GetSalarys(String accountnumber) {
		RestTemplate restTemplate = new RestTemplate();
		
		String result = "";
		String result_encrypted = restTemplate.postForObject(REST_SERVICE_URI + "/getSalarys?accountnumber={accountnumber}&isGetAllProject=true&fyg=fyg", null, String.class, accountnumber);
		try {
			result = DES.decryptDES(result_encrypted, DES.key);
		} catch (Exception e) {
			System.out.println("error");
		}
		
		return result;
	}
	
	private String doPost(SearchBean bean) {
		RestTemplate restTemplate = new RestTemplate();
		
		String result = "";
		if (!bean.getAccountnumber().isEmpty() && bean.getProjectnumber().isEmpty()) {
			String result_encrypted = restTemplate.postForObject(REST_SERVICE_URI + "/getSalarys?accountnumber={accountnumber}&isGetAllProject=true&fyg=fyg", null, String.class, bean.getAccountnumber());
			try {
				result = DES.decryptDES(result_encrypted, DES.key);
			} catch (Exception e) {
				System.out.println("error");
			}
		} else if (!bean.getAccountnumber().isEmpty() && !bean.getProjectnumber().isEmpty()) {
			String result_encrypted = restTemplate.postForObject(REST_SERVICE_URI + "/getSalarys?accountnumber={accountnumber}&projectnumber={projectnumber}&isGetAllProject=false&fyg=fyg", null, String.class, bean.getAccountnumber(), bean.getProjectnumber());
			try {
				result = DES.decryptDES(result_encrypted, DES.key);
			} catch (Exception e) {
				System.out.println("error");
			}
		}
		
		
		return result;
	}

	@Override
	public Retv<List<ProjectDto>> getAllProject(String accountnumber) {
		// TODO Auto-generated method stub
		String result = this.GetSalarys(accountnumber);
		if (result.contains("detail")) {
			JsonElement je = Streams.parse(new JsonReader(new StringReader(result)));
			JsonObject jo = je.getAsJsonObject();
			List<ProjectDto> projects = GsonUtil.fromJsonList(jo.get("detail").toString(), ProjectDto.class);
			return Retv.data(projects);
		}
		return Retv.error("当前账户下没有数据");
	}
	
	@Override
	public Retv<SalaryDto> getSalary(String acNum, String pjNum) {
		// TODO Auto-generated method stub
		SearchBean bean = new SearchBean();
		bean.setAccountnumber(acNum);
		bean.setProjectnumber(pjNum);
		String result = this.doPost(bean);
		if (result.contains("detail")) {
			JsonElement je = Streams.parse(new JsonReader(new StringReader(result)));
			JsonObject jo = je.getAsJsonObject();
			List<SalarydetailDto> detail = GsonUtil.fromJsonList(jo.get("detail").toString(), SalarydetailDto.class);
			SalaryDto salary = new SalaryDto();
			salary.setDetail(detail);
			salary.setRate(jo.get("rate").toString());
			salary.setTotalReceivedAmount(jo.get("totalReceivedAmount").toString());
			salary.setTotalSalary(jo.get("totalSalary").toString());
			return Retv.data(salary);
		}
		return Retv.error("当前项目下没有数据");
	}

	@Override
	public String test() {
		// TODO Auto-generated method stub
		return null;
	}

}
